//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "RiskyAssetSwapOption.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Experimental/Credit/RiskyAssetSwap.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instrument.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CRiskyAssetSwapOption::CRiskyAssetSwapOption (Boolean payer, Cephei::QL::Experimental::Credit::IRiskyAssetSwap^ asw, DateTime expiry, Double marketSpread, Double spreadVolatility, Cephei::QL::IPricingEngine^ QL_Pricer) : CInstrument(CRiskyAssetSwapOption::typeid)
{
    CRiskyAssetSwap^ _Casw;
    try
    {
#ifdef HANDLE
        _phRiskyAssetSwapOption = NULL;
#endif
        bool _payer = (bool)ValueHelper::Convert (payer); //d
        _Casw = safe_cast<CRiskyAssetSwap^> (asw);
        _Casw->Lock();
        boost::shared_ptr<QuantLib::RiskyAssetSwap>& _asw = static_cast<boost::shared_ptr<QuantLib::RiskyAssetSwap>&> (_Casw->GetShared ()); 
        QuantLib::Date _expiry = (QuantLib::Date)ValueHelper::Convert (expiry); //d
        QuantLib::Rate _marketSpread = (QuantLib::Rate)ValueHelper::Convert (marketSpread); //d
        QuantLib::Volatility _spreadVolatility = (QuantLib::Volatility)ValueHelper::Convert (spreadVolatility); //d
        _ppRiskyAssetSwapOption = new boost::shared_ptr<QuantLib::RiskyAssetSwapOption> (new QuantLib::RiskyAssetSwapOption ( _payer,  _asw,  _expiry,  _marketSpread,  _spreadVolatility ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppRiskyAssetSwapOption)->setPricingEngine (_QL_Pricer);
        SetInstrument (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyAssetSwapOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Casw != nullptr) _Casw->Unlock();
    }
}
Cephei::QL::Experimental::Credit::CRiskyAssetSwapOption::CRiskyAssetSwapOption (boost::shared_ptr<QuantLib::RiskyAssetSwapOption>& childNative, Object^ owner) : CInstrument(CRiskyAssetSwapOption::typeid)
{
#ifdef HANDLE
	_phRiskyAssetSwapOption = NULL;
#endif
	_ppRiskyAssetSwapOption = &childNative;
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyAssetSwapOption));
}
Cephei::QL::Experimental::Credit::CRiskyAssetSwapOption::CRiskyAssetSwapOption (QuantLib::RiskyAssetSwapOption& childNative, Object^ owner) : CInstrument(CRiskyAssetSwapOption::typeid)
{
#ifdef HANDLE
	_phRiskyAssetSwapOption = NULL;
#endif
	_ppRiskyAssetSwapOption = new boost::shared_ptr<QuantLib::RiskyAssetSwapOption> (&childNative);
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyAssetSwapOption));
    _RiskyAssetSwapOptionOwner = owner;
    _InstrumentOwner = owner;
}

Cephei::QL::Experimental::Credit::CRiskyAssetSwapOption::CRiskyAssetSwapOption (CRiskyAssetSwapOption^ copy) : CInstrument(CRiskyAssetSwapOption::typeid)
{
#ifdef HANDLE
	_phRiskyAssetSwapOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppRiskyAssetSwapOption = new boost::shared_ptr<QuantLib::RiskyAssetSwapOption> (copy->GetShared());
        _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyAssetSwapOption));
    }
}
Cephei::QL::Experimental::Credit::CRiskyAssetSwapOption::CRiskyAssetSwapOption (PLATFORM::Type^ t) : CInstrument(CRiskyAssetSwapOption::typeid)
{
#ifdef HANDLE
	_phRiskyAssetSwapOption = NULL;
#endif
	if (!t->IsSubclassOf(CRiskyAssetSwapOption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CRiskyAssetSwapOption::CRiskyAssetSwapOption (QuantLib::Handle<QuantLib::RiskyAssetSwapOption>& childNative, Object^ owner)  : CInstrument(CRiskyAssetSwapOption::typeid)
{
	_phRiskyAssetSwapOption = &childNative;
	_ppRiskyAssetSwapOption = &static_cast<boost::shared_ptr<QuantLib::RiskyAssetSwapOption>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyAssetSwapOption));
    _RiskyAssetSwapOptionOwner = owner;
}
Cephei::QL::Experimental::Credit::CRiskyAssetSwapOption::CRiskyAssetSwapOption (QuantLib::Handle<QuantLib::RiskyAssetSwapOption> childNative)  : CInstrument(CRiskyAssetSwapOption::typeid)
{
	_phRiskyAssetSwapOption = &childNative;
	_ppRiskyAssetSwapOption = &static_cast<boost::shared_ptr<QuantLib::RiskyAssetSwapOption>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyAssetSwapOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CRiskyAssetSwapOption::CRiskyAssetSwapOption (QuantLib::RiskyAssetSwapOption childNative)  : CInstrument(CRiskyAssetSwapOption::typeid)
{
#ifdef HANDLE
	_phRiskyAssetSwapOption = NULL;
#endif
	_ppRiskyAssetSwapOption = new boost::shared_ptr<QuantLib::RiskyAssetSwapOption> (new QuantLib::RiskyAssetSwapOption (childNative));
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyAssetSwapOption));
}
#endif

Cephei::QL::Experimental::Credit::CRiskyAssetSwapOption::~CRiskyAssetSwapOption ()
{
    if (_ppRiskyAssetSwapOption != NULL)
    {
	    delete _ppRiskyAssetSwapOption;
        _ppRiskyAssetSwapOption = NULL;
    }
}
Cephei::QL::Experimental::Credit::CRiskyAssetSwapOption::!CRiskyAssetSwapOption ()
{
    if (_ppRiskyAssetSwapOption != NULL)
    {
	    delete _ppRiskyAssetSwapOption;
    }
}
QuantLib::RiskyAssetSwapOption& Cephei::QL::Experimental::Credit::CRiskyAssetSwapOption::GetReference ()
{
    if (_ppRiskyAssetSwapOption == NULL) throw REFNEW NativeNullException ();
	return **_ppRiskyAssetSwapOption;
}
boost::shared_ptr<QuantLib::RiskyAssetSwapOption>& Cephei::QL::Experimental::Credit::CRiskyAssetSwapOption::GetShared ()
{
    if (_ppRiskyAssetSwapOption == NULL) throw REFNEW NativeNullException ();
	return *_ppRiskyAssetSwapOption;
}
QuantLib::RiskyAssetSwapOption* Cephei::QL::Experimental::Credit::CRiskyAssetSwapOption::GetPointer ()
{
    if (_ppRiskyAssetSwapOption == NULL) throw REFNEW NativeNullException ();
	return &**_ppRiskyAssetSwapOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::RiskyAssetSwapOption>& Cephei::QL::Experimental::Credit::CRiskyAssetSwapOption::GetHandle ()
{
	if (_phRiskyAssetSwapOption == NULL)
	{
		_phRiskyAssetSwapOption = new Handle<QuantLib::RiskyAssetSwapOption> (*_ppRiskyAssetSwapOption);
	}
	return *_phRiskyAssetSwapOption;
}
#endif
bool Cephei::QL::Experimental::Credit::CRiskyAssetSwapOption::HasNative () 
{
	return (_ppRiskyAssetSwapOption != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::IRiskyAssetSwapOption^ Cephei::QL::Experimental::Credit::CRiskyAssetSwapOption_Factory::Create (Boolean payer, Cephei::QL::Experimental::Credit::IRiskyAssetSwap^ asw, DateTime expiry, Double marketSpread, Double spreadVolatility, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CRiskyAssetSwapOption ( payer,  asw,  expiry,  marketSpread,  spreadVolatility,  QL_Pricer);
}
